package longshu.data.service;

import longshu.data.dao.ProvinceCityAreaDao;
import longshu.data.domain.ProvinceCityArea;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.List;

/**
 * ProvinceCityAreaService
 *
 * @author LongShu 2018-02-03
 */
@javax.inject.Named
@CacheConfig(cacheNames = "ProvinceCityArea")
public class ProvinceCityAreaService extends BaseService<ProvinceCityAreaDao, ProvinceCityArea> {

    /**
     * 查询省列表
     */
    @Cacheable(key = "#methodName")
    public List<ProvinceCityArea> queryProvinceList() {
        Example example = Example.builder(ProvinceCityArea.class)
                .where(Sqls.custom().andEqualTo("yn", true).andIsNull("pc"))
                .orderByAsc("code")
                .build();

        return getDao().selectByExample(example);
    }

    /**
     * 查询地区列表
     *
     * @param pc 省/市代码
     */
    @Cacheable(key = "#methodName+''+#p0")
    public List<ProvinceCityArea> queryRegionList(Integer pc) {
        Example example = Example.builder(ProvinceCityArea.class)
                .where(Sqls.custom().andEqualTo("yn", true).andEqualTo("pc", pc))
                .orderByAsc("code")
                .build();

        return getDao().selectByExample(example);
    }

    /**
     * 查询单个省市区
     *
     * @param code 行政代码
     */
    @Cacheable(key = "#methodName+''+#p0")
    public ProvinceCityArea queryAddress(Integer code) {
        ProvinceCityArea address = new ProvinceCityArea(code, null);
        address = getDao().selectOne(address);
        return address;
    }

}
